﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Bestfc.DBUtility;
using System.Data;

namespace Authority.Dal
{
    public class PermissionDal
    {
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Permission");
            strSql.Append(" where Id=" + Id + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Authority.Model.PermissionModel model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.PermissionType != null)
            {
                strSql1.Append("PermissionType,");
                strSql2.Append("'" + model.PermissionType + "',");
            }
            if (model.PermissionValue != null)
            {
                strSql1.Append("PermissionValue,");
                strSql2.Append("" + model.PermissionValue + ",");
            }
            strSql.Append("insert into Permission(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Authority.Model.PermissionModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Permission set ");
            if (model.PermissionType != null)
            {
                strSql.Append("PermissionType='" + model.PermissionType + "',");
            }
            if (model.PermissionValue != null)
            {
                strSql.Append("PermissionValue=" + model.PermissionValue + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where Id=" + model.Id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Permission ");
            strSql.Append(" where Id=" + Id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Authority.Model.PermissionModel GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" Id,PermissionType,PermissionValue ");
            strSql.Append(" from Permission ");
            strSql.Append(" where Id=" + Id + " ");
            Authority.Model.PermissionModel model = new Authority.Model.PermissionModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (dr["Id"].ToString() != "")
                {
                    model.Id = int.Parse(dr["Id"].ToString());
                }
                model.PermissionType = dr["PermissionType"].ToString();
                if (dr["PermissionValue"].ToString() != "")
                {
                    model.PermissionValue = int.Parse(dr["PermissionValue"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,PermissionType,PermissionValue ");
            strSql.Append(" FROM Permission ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,PermissionType,PermissionValue ");
            strSql.Append(" FROM Permission ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}